package org.example.settings.service.impl;

import org.example.commons.constant.Constant;
import org.example.commons.utils.DateFormatUtils;
import org.example.commons.utils.UUIDUtils;
import org.example.settings.domain.Clue;
import org.example.settings.domain.User;
import org.example.settings.mapper.ClueMapper;
import org.example.settings.service.ClueService;
import org.example.workbench.domain.*;
import org.example.workbench.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service("ClueService")
public class ClueImpl implements ClueService {

    @Autowired
    private ClueMapper clueMapper;
    @Autowired
    private ContactsMapper contactsMapper;
    @Autowired
    private CustomerRemarkMapper customerRemarkMapper;
    @Autowired
    private ClueRemarkMapper clueRemarkMapper;
    @Autowired
    private ContactsRemarkMapper contactsRemarkMapper;
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private ClueActivityRelationMapper clueActivityRelationMapper;
    @Autowired
    private ContactsActivityRelationMapper contactsActivityRelationMapper;
    @Autowired
    private TranMapper tranMapper;
    @Autowired
    private TranRemarkMapper tranRemarkMapper;

    @Override
    public void deleteClueByClueId(String id) {
        //删除该线索下的所有备注
        clueRemarkMapper.deleteRemarkByClueId(id);
        //删除该线索下的所有关联关系活动
        clueActivityRelationMapper.deleteRelationByClueId(id);
        //删除该线索
        clueMapper.deleteClueById(id);
    }

    @Override
    public int updateClueByMap(Map<String, Object> map) {
        return clueMapper.updateClueByMap(map);
    }

    @Override
    public Clue getClueDetailById(String id) {
        return clueMapper.getClueDetailById(id);
    }

    @Override
    public int getTotalCountClue() {
        return clueMapper.getTotalCountClue();
    }

    @Override
    public List<Clue> getAllClue(Map<String,Object> map) {
        return clueMapper.getAllClue(map);
    }

    @Override
    public void saveCustomerFromDetail(Map<String, Object> map) {
        String clueId = (String)map.get("clueId");
        User user = (User)map.get(Constant.SESSION_USER);
        Clue clue = clueMapper.getAllDetailById(clueId);
        Customer customer = new Customer();
        customer.setId(UUIDUtils.getUUID());
        customer.setOwner(user.getId());
        customer.setName(clue.getCompany());
        customer.setWebsite(clue.getWebsite());
        customer.setPhone(clue.getPhone());
        customer.setCreateBy(user.getId());
        customer.setCreateTime(DateFormatUtils.formatDateTime(new Date()));
        customer.setContactSummary(clue.getContactSummary());
        customer.setNextContactTime(clue.getNextContactTime());
        customer.setDescription(clue.getDescription());
        customer.setAddress(clue.getAddress());
        customerMapper.insertCustomerByCustomer(customer);
        Contacts contact = new Contacts();
        contact.setId(UUIDUtils.getUUID());
        contact.setOwner(user.getId());
        contact.setSource(clue.getSource());
        contact.setCustomerId(customer.getId());
        contact.setFullname(clue.getFullname());
        contact.setAppellation(clue.getAppellation());
        contact.setEmail(clue.getEmail());
        contact.setMphone(clue.getMphone());
        contact.setJob(clue.getJob());
        contact.setCreateBy(user.getId());
        contact.setCreateTime(DateFormatUtils.formatDateTime(new Date()));
        contact.setDescription(clue.getDescription());
        contact.setContactSummary(clue.getContactSummary());
        contact.setNextContactTime(clue.getNextContactTime());
        contact.setAddress(clue.getAddress());
        contactsMapper.insertContactByContact(contact);
        List<ClueRemark> clueRemarks = clueRemarkMapper.findClueRemarkDetailById(clueId);
        if (clueRemarks!=null && clueRemarks.size()!=0){
            List<CustomerRemark> customerRemarkList = new ArrayList<CustomerRemark>();
            ArrayList<ContactsRemark> contactsRemarkList = new ArrayList<ContactsRemark>();
            CustomerRemark customerRemark =null;
            ContactsRemark contactsRemark =null;
            for (ClueRemark remark : clueRemarks) {
                customerRemark = new CustomerRemark();
                customerRemark.setId(UUIDUtils.getUUID());
                customerRemark.setNoteContent(remark.getNoteContent());
                customerRemark.setCreateBy(remark.getCreateBy());
                customerRemark.setCreateTime(remark.getCreateTime());
                customerRemark.setEditBy(remark.getEditBy());
                customerRemark.setEditTime(remark.getEditTime());
                customerRemark.setEditFlag(remark.getEditFlag());
                customerRemark.setCustomerId(customer.getId());
                customerRemarkList.add(customerRemark);

                contactsRemark = new ContactsRemark();
                contactsRemark.setId(UUIDUtils.getUUID());
                contactsRemark.setNoteContent(remark.getNoteContent());
                contactsRemark.setCreateBy(remark.getCreateBy());
                contactsRemark.setCreateTime(remark.getCreateTime());
                contactsRemark.setEditBy(remark.getEditBy());
                contactsRemark.setEditTime(remark.getEditTime());
                contactsRemark.setEditFlag(remark.getEditFlag());
                contactsRemark.setContactsId(contact.getId());
                contactsRemarkList.add(contactsRemark);
            }
            customerRemarkMapper.insertManyByList(customerRemarkList);
            contactsRemarkMapper.insertManyByList(contactsRemarkList);
        }
        //从表tbl_clue_activity_relation查询所有和相应线索id相关的信息
        List<ClueActivityRelation> clueRelation = clueActivityRelationMapper.findClueActivityRelationDetailById(clueId);
        if (clueRelation!=null && clueRelation.size()!=0){
            ContactsActivityRelation contactsRelation =null;
            ArrayList<ContactsActivityRelation> contactsActivityRelationList = new ArrayList<>();
            for (ClueActivityRelation relation : clueRelation) {
                contactsRelation = new ContactsActivityRelation();
                contactsRelation.setId(UUIDUtils.getUUID());
                contactsRelation.setContactsId(contact.getId());
                contactsRelation.setActivityId(relation.getActivityId());
                //将线索和市场活动的信息数据存放在集合中
                contactsActivityRelationList.add(contactsRelation);
            }
            //将所有数据插入联系人和市场活动表中
            contactsActivityRelationMapper.insertManyByList(contactsActivityRelationList);
        }
        //如果要创建交易，则判断参数，插入数据,还要将备注信息插入
        String isChecked = (String) map.get("isChecked");
        if("true".equals(isChecked)){
            Tran tran = new Tran();
            tran.setId(UUIDUtils.getUUID());
            tran.setOwner(user.getId());
            tran.setMoney((String)map.get("money"));
            tran.setName((String)map.get("name"));
            tran.setExpectedDate((String)map.get("expectedDate"));
            tran.setCustomerId(customer.getId());
            tran.setStage((String)map.get("stage"));
            tran.setSource((String)map.get("source"));
            tran.setActivityId((String)map.get("activityId"));
            tran.setContactsId(contact.getId());
            tran.setCreateBy(user.getId());
            tran.setCreateTime(DateFormatUtils.formatDateTime(new Date()));
            tranMapper.insertOneByTran(tran);

            if (clueRemarks!=null && clueRemarks.size()!=0){
                ArrayList<TranRemark> tranRemarkList = new ArrayList<>();
                for (ClueRemark remark : clueRemarks) {
                    TranRemark tranRemark = new TranRemark();
                    tranRemark.setId(UUIDUtils.getUUID());
                    tranRemark.setNoteContent(remark.getNoteContent());
                    tranRemark.setCreateBy(remark.getCreateBy());
                    tranRemark.setCreateTime(remark.getCreateTime());
                    tranRemark.setEditBy(remark.getEditBy());
                    tranRemark.setEditTime(remark.getEditTime());
                    tranRemark.setEditFlag(remark.getEditFlag());
                    tranRemark.setTranId(tran.getId());
                    tranRemarkList.add(tranRemark);
                }
                tranRemarkMapper.insertManyByList(tranRemarkList);
            }
        }
        //删除该线索下的所有备注
        clueRemarkMapper.deleteRemarkByClueId(clueId);
        //删除该线索下的市场活动的关联关系
        clueActivityRelationMapper.deleteRelationByClueId(clueId);
        //删除该线索
        clueMapper.deleteClueById(clueId);
    }

    @Override
    public Clue getClueById(String id) {
        return clueMapper.getClueById(id);
    }

    @Override
    public int insertOneClueByClue(Clue clue) {
        return clueMapper.insertOneClueByClue(clue);
    }
}
